﻿@using KPIS.GERP.Models.ViewModel
@using KPIS.GERP.Models.ICM
@using KPIS.GERP.Models
@model IEnumerable<BidderQuotation>

@{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    ViewBag.Title = "ข้อมูล เสนอราคา";
    ViewBag.Descr = "ข้อมูล เสนอราคา";

    int? intBidderId = ViewBag.BidderId;
}

@using (Html.BeginForm("DeleteAll", "BidderQuotation", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="row-fluid">
        <div class="block-button-menu-above">
            @if (ViewBag.bEditable && !ViewBag.IsFromOther)
            {
                if (ViewBag.BidModel != null)
                {
                    <div class="alert alert-info">
                        <h5>
                            <i class="icon-warning-sign"></i>ช่วงเวลาที่กำหนดให้เสนอราคาคือ
                            @{
                    var bidModel = ViewBag.BidModel as Bid;
                            }
                            ระหว่าง @(bidModel != null && bidModel.OfferDateTimeStart != null ? bidModel.OfferDateTimeStart.Value.ToString("d MMMM yyyy hh:mm น.") : "[ไม่กำหนด]")  - @(bidModel != null && bidModel.OfferDateTimeEnd != null ? bidModel.OfferDateTimeEnd.Value.ToString("d MMMM yyyy hh:mm น.") : "[ไม่กำหนด]")

                        </h5>
                    </div>
                }
                @*@Html.ActionLink("เพิ่ม", "Create", new { BidderId = intBidderId }
                    , new { @class = "btn btn-small btn-success button-standard-width", role = "button" })

                    @Html.ButtonDeleteAll()*@
            }
            else if (!ViewBag.IsFromOther && ViewBag.BidModel as Bid != null)
            {
                <div class="alert alert-error">
                    <h5>
                        <i class="icon-warning-sign"></i> ไม่สามารถแก้ไขรายการได้ เนื่องจากไม่อยู่ในช่วงเวลาที่กำหนด
                        @{
                var bidModel = ViewBag.BidModel as Bid;

                        }
                        (ระหว่าง @(bidModel != null && bidModel.OfferDateTimeStart != null ? bidModel.OfferDateTimeStart.Value.ToString("d MMMM yyyy hh:mm น.") : "[ไม่กำหนด]")  - @(bidModel != null && bidModel.OfferDateTimeEnd != null ? bidModel.OfferDateTimeEnd.Value.ToString("d MMMM yyyy hh:mm น.") : "[ไม่กำหนด]"))

                    </h5>
                </div>
            }
        </div>

        <div class="alert-error">@Html.ValidationSummary()</div>

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                    @ViewBag.Title
                </div>
                @Html.Partial("_ExportButton")
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    @*<th class="center" style="width:50px;">
                            <label>
                                <input type="checkbox" />
                                <span class="lbl"></span>
                            </label>
                        </th>*@
                    @if (ViewBag.BidModel != null && !ViewBag.IsFromOther)
                    {
                        <th style="width:70px">
                            ถูกเลือก@*Column#1*@
                        </th>
                    }
                    @*<th>*@
                        @*รหัสกิจกรรม*@@*Column#2*@

                    @*</th>*@
                            
                            <th>
                                แหล่งเงิน
                            </th>
                            <th>
                                สินค้า/บริการ@*Column#2*@
                            </th>
                            <th>
                                รายละเอียด@*Column#3*@
                            </th>
                            <th>
                                จำนวน@*Column#4*@
                            </th>
                            <th>
                                ราคาต่อหน่วย@*Column#5*@
                            </th>
                            <th>
                                ราคาไม่รวม Vat@*Column#6*@
                            </th>
                            <th>
                                Vat 7%@*Column#7*@
                            </th>
                            @*<th>
                                    ส่วนลด
                                </th>
                                <th>
                                    ส่วนลดพิเศษ
                                </th>*@
                            <th>
                                ราคาสุทธิ@*Column#8*@
                            </th>
                            <th class="center" style="width:80px;">
                                <!-- Empty head column for export buttons -->&nbsp;@*Column#9*@
                            </th>
                        </tr>
                    </thead>
                    @{
                    GERPContext db = new GERPContext();
                    var pprItems = db.PrePurchaseRequestItems
                        .Include("ProjectOperationExpense.ProjectExpense.MasBudgetSource")
                        .Include("ProjectOperationExpense.ProjectExpense.Project")
                        .ToList();

                    var projectService = new KPIS.GERP.WEB.GIMS.ProjectOperationService.ProjectOperationServiceClient();
                    }
                    <tbody>
                        @foreach (var m in Model ?? new List<BidderQuotation>())
                        {
                            <tr>
                                @*<td class="center" style="width:50px;">
                                        <label>
                                            <input type="checkbox" name="deleteIds" value="@(m.BidderQuoId)" />
                                            <span class="lbl"></span>
                                        </label>
                                    </td>*@
                                @if (ViewBag.BidModel != null && !ViewBag.IsFromOther)
                                {
                                    <td style="text-align:center" style="width:70px;">
                                        @switch (m.Selected)
                                        {
                                            case "0":
                                                <label>ยังไม่เลือก</label>
                                                break;
                                            case "1":
                                            <i class="icon-ok bigger-150 green"></i>
                                                break;
                                            case "2":
                                            <i class="icon-remove bigger-150 red"></i>
                                                break;
                                            default:
                                                break;
                                        }@*Column#1*@
                                    </td>
                                }
                                @*<td>
                                        @{
                                        var pprItem = pprItems.FirstOrDefault(o => o.PrItemId == m.PrItemId);
                                        int ProjId =
                                            pprItem.ProjectOperationExpense.ProjectExpense.Project.ProjId;
                                        Decimal TotalAmount =
                                            projectService.SelectProjectExpense(new ProjectExpense { ProjId = ProjId }, null)
                                                .ToList()
                                                .Sum(pe => pe.TotalAmount) ?? 0;
                                        string StrBgmName =
                                            pprItem.ProjectOperationExpense.ProjectExpense.Project.Name;
                                        string[] StrC = StrBgmName.Split(')');
                                        string BgmName = StrC[0];
                                        string BgmNo = StrC.Count() > 1 ? StrC[1] : "";
                                        @:@BgmNo
                                        }

                                    </td>*@
                                <td>
                                    @*@{
                                        var pprItem = pprItems.FirstOrDefault(o => o.PrItemId == m.PrItemId);
                                        int ProjId =
                                            pprItem.ProjectOperationExpense.ProjectExpense.Project.ProjId;
                                        Decimal TotalAmount =
                                            projectService.SelectProjectExpense(new ProjectExpense { ProjId = ProjId }, null)
                                                .ToList()
                                                .Sum(pe => pe.TotalAmount) ?? 0;
                                        string StrBgmName =
                                            pprItem.ProjectOperationExpense.ProjectExpense.Project.Name;
                                        string[] StrC = StrBgmName.Split(')');
                                        string BgmName = StrC[0];
                                        string BgmNo = StrC.Count() > 1 ? StrC[1] : "";
                                    }*@
                                    @{
                            var pprItem = pprItems.FirstOrDefault(o => o.PrItemId == m.PrItemId);
                            if (pprItem != null &&
                                        pprItem.ProjectOperationExpense != null &&
                                        pprItem.ProjectOperationExpense.ProjectExpense != null &&
                                        pprItem.ProjectOperationExpense.ProjectExpense.MasBudgetSource != null)
                            {
                                @:@pprItem.ProjectOperationExpense.ProjectExpense.MasBudgetSource.Name
                                }
                                    }
                                </td>
                                <td>
                                    @*@{
                                            var stringA = (m.PurchaseRequestItem != null && m.PurchaseRequestItem.ResItemId != null && m.PurchaseRequestItem.MasResourceItem != null ? @m.PurchaseRequestItem.MasResourceItem.Name : (m.PurchaseRequestItem != null && m.PurchaseRequestItem.MasBudgetExpense != null ?  @m.PurchaseRequestItem.MasBudgetExpense.Name : ""));
                                            }

                                        @if(!string.IsNullOrEmpty(stringA)) {
                                        @Html.ActionLink(stringA
                                            , "Details", new { Id = m.BidderQuoId, IsFromOther = (bool)ViewBag.IsFromOther })
                                        }*@
                                    @{
                            var stringA = (m.PurchaseRequestItem.HaveItem ?? true) ?
                                m.PurchaseRequestItem.ResItemId != null ? m.PurchaseRequestItem.MasResourceItem.Name : m.PurchaseRequestItem.MasBudgetExpense.Name
                                : "ไม่ระบุ";
                                    }

                                    @if (!string.IsNullOrEmpty(stringA))
                                    {
                                        @Html.ActionLink(stringA, "Details", new { Id = m.BidderQuoId, IsFromOther = (bool)ViewBag.IsFromOther })
                                    }@*Column#2*@
                                </td>
                                <td>
                                    @Html.DisplayFor(model => m.Descr)@*Column#3*@
                                </td>
                                <td style="text-align:right">
                                    @Html.DisplayFor(model => m.Qty)@*Column#4*@
                                </td>
                                <td style="text-align:right">
                                    @Html.DisplayFor(model => m.Price)@*Column#5*@
                                </td>
                                <td style="text-align:right">
                                    @Html.DisplayFor(model => m.ExcludeVatAmount)@*Column#6*@
                                </td>
                                <td style="text-align:right">
                                    @Html.DisplayFor(model => m.VatAmount)@*Column#7*@
                                </td>
                                @*<td style="text-align:right">
                                        @Html.DisplayFor(model => m.DiscountPercent)
                                    </td>
                                    <td style="text-align:right">
                                        @Html.DisplayFor(model => m.ExtraDiscount)
                                    </td>*@
                                <td style="text-align:right">
                                    @Html.DisplayFor(model => m.NetAmount)@*Column#8*@
                                </td>
                                <td class="td-actions">
                                    @if (ViewBag.bEditable && !ViewBag.IsFromOther)
                                    {
                                        @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                {
                                    Id = m.BidderQuoId,
                                    DetailsUrl = Url.Action("Details", new { Id = m.BidderQuoId, IsFromOther = (bool)ViewBag.IsFromOther }),
                                    EditUrl = Url.Action("Edit") + "/" + m.BidderQuoId,
                                    //DeleteUrl = Url.Action("Delete") + "/" + m.BidderQuoId
                                })
                                    }
                                    else
                                    {
                                        @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                {
                                    Id = m.BidderQuoId,
                                    DetailsUrl = Url.Action("Details", new { Id = m.BidderQuoId, IsFromOther = (bool)ViewBag.IsFromOther }),
                                })
                                    }@*Column#9*@
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
                <br />
                <div style="font-size:large;color:blue;text-align:right;padding-right:7em;">
                    ยอดรวมทั้งหมด = @(((Model ?? new List<BidderQuotation>()).Sum(o => o.NetAmount) ?? 0).ToString("#,##0.00"))
                </div>
            </div>
}

@section scripts {

    <script type="text/javascript">
        $(function () {
            var oTable1 = $('#table').dataTable({
                "aoColumns": [
                    @*{ "bSortable": false },*@
                    @if (ViewBag.BidModel != null)
				    {
                        Html.Raw("null,");
                    }
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
                ]
            });

            $('table th input:checkbox').on('click', function () {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
				    this.checked = that.checked;
				    $(this).closest('tr').toggleClass('selected');
				});

            });

            $('[data-rel=tooltip]').tooltip();

            $("form").submit(function () {
                var currentForm = this;
                bootbox.confirm(function (result) {
                    var _result = result;
                    if (result) {
                        $.ajax({
                            url: '@(Url.Action("DeleteAll", "BidderQuotation"))',
                            type: "post",
                            dataType: "json",
                            data: $(currentForm).serialize(),
                            success: function (result) {
                                if (result.success) {
                                    location.reload();
                                }
                                else {
                                }
                            }
                        });
                    }
                })
                return false;

            });

        })

    </script>
}